package com.caxxb.dao.impl;

import com.caxxb.dao.OrderDao;
import com.caxxb.entity.Game;

import com.caxxb.entity.Order;
import com.caxxb.entity.OrderPojo;
import com.caxxb.util.DBUtil;
import com.caxxb.util.PageData;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    //增加订单的方法
    @Override
    public boolean addToOrder(Game game, int userid,String conversion_code) {
        String sql="insert into `order` values(null,?,?,?,?,?)";
//        LocalDateTime.now().format(JacksonObjectMapper.DEFAULT_DATE_TIME_FORMAT);
        LocalDateTime now = LocalDateTime.now();
        String time = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        int result = DBUtil.exUpdate(sql,game.getGame_id(), time,userid,game.getPrice(),conversion_code);
        return result>0;

    }

    @Override
    public PageData<OrderPojo> getOrdersByUserId(int userid, int pageNo, int pageSize) {
        String sql="select o.*,g.game_name,g.game_img from `order` o INNER JOIN game g where o.game_id=g.game_id and o.user_id=? order by o.create_time desc";
        PageData pageData = DBUtil.exQueryByPage(sql, OrderPojo.class, pageNo, pageSize,userid);
        return pageData;
    }

    @Override
    public PageData<OrderPojo> queryOrderByPage(int pageNo, int pageSize, String keywords) {
        String sql = "SELECT `order`.order_id, `order`.game_id, `order`.create_time, `order`.user_id, `order`.order_price, game.game_name, `user`.user_name FROM `order` INNER JOIN game ON `order`.game_id = game.game_id INNER JOIN `user` ON `order`.user_id = `user`.user_id where order_id like ?";
        PageData pageData = DBUtil.exQueryByPage(sql, OrderPojo.class, pageNo, pageSize,keywords );
        return pageData;
    }

    @Override
    public List<Order> CheckOrderByUserId(int userid, int gameid) {

        String sql="select game_id from `order` where user_id=?";
        List<Order> list = DBUtil.exQuery(sql, Order.class, userid);
        return list;
    }

    @Override
    public OrderPojo getOrderByOrderId(int orderid) {
        String sql="select * from `order` where order_id=?";
        List<OrderPojo> list = DBUtil.exQuery(sql, OrderPojo.class, orderid);
        OrderPojo orderPojo = list.get(0);
        return orderPojo;
    }

    @Override
    public boolean delOrderByOrderId(int orderid) {
        String sql="delete from `order` where order_id=?";
        int result = DBUtil.exUpdate(sql, orderid);
        return result >0;
    }
}